package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class ProjectDeliveryDetailService {

	@Autowired
	private ProjectDeliveryDetailDao projectDeliveryDetailDao;

	private QProjectDeliveryDetail $ = QProjectDeliveryDetail.projectDeliveryDetail;

	@Autowired
	public void setProjectDeliveryDetailDao(
			ProjectDeliveryDetailDao projectDeliveryDetailDao) {
		this.projectDeliveryDetailDao = projectDeliveryDetailDao;
	}

	public List<ProjectDeliveryDetail> findAll() {
		return ListUtil.toList(projectDeliveryDetailDao.findAll());
	}

	public List<ProjectDeliveryDetail> findAll(Long projectDeliveryIdId,
			String productSequenceNo, String eTag, String remark,
			String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(projectDeliveryIdId)) {
			condition.and($.projectDelivery.id.eq(projectDeliveryIdId));
		}

		if (StringUtil.isNotEmpty(productSequenceNo)) {
			condition.and($.productSequenceNo.contains(productSequenceNo));
		}

		if (StringUtil.isNotEmpty(eTag)) {
			condition.and($.eTag.contains(eTag));
		}

		if (StringUtil.isNotEmpty(remark)) {
			condition.and($.remark.contains(remark));
		}

		return ListUtil.toList(projectDeliveryDetailDao.findAll(condition));
	}

	public Page<ProjectDeliveryDetail> findAll(Long projectDeliveryId,
			String productSequenceNo, String eTag, String remark,
			int pageNumber, int pageSize, String sortAttribute,
			String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(projectDeliveryId)) {
			condition.and($.projectDelivery.id.eq(projectDeliveryId));
		}
		if (StringUtil.isNotEmpty(productSequenceNo)) {
			condition.and($.productSequenceNo.contains(productSequenceNo));
		}
		if (StringUtil.isNotEmpty(eTag)) {
			condition.and($.eTag.contains(eTag));
		}
		if (StringUtil.isNotEmpty(remark)) {
			condition.and($.remark.contains(remark));
		}

		Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection), sortAttribute);
		}

		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject);
		return projectDeliveryDetailDao.findAll(condition, pageRequest);
	}

	public void deleteProjectDeliveryDetail(Long id) {
		projectDeliveryDetailDao.delete(id);
	}

	public void saveProjectDeliveryDetail(
			ProjectDeliveryDetail projectDeliveryDetail) {
		projectDeliveryDetailDao.save(projectDeliveryDetail);
	}

	public Page<ProjectDeliveryDetail> findByProjectDeliveryId(
		Long projectDeliveryIdId,
		int pageNumber, int pageSize, String sortAttribute,
		String orderDirection) {
	BooleanBuilder condition = new BooleanBuilder();

	if (IdUtil.isNotEmpty(projectDeliveryIdId)) {
		condition.and($.projectDelivery.id.eq(projectDeliveryIdId));
	}

	Sort sortObject = null;
	if (StringUtil.isEmpty(sortAttribute)) {
		sortObject = new Sort(Direction.DESC, "id");
	} else {
		sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
				sortAttribute);
	}

	PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
			sortObject);
	return projectDeliveryDetailDao.findAll(condition, pageRequest);
	}

	public ProjectDeliveryDetail findOne(Long id) {
		return this.projectDeliveryDetailDao.findOne(id);
	}

}
